<?php
namespace app\common\model;

use think\Model;

/**
 * 管理员模型
 * Class AdminUser
 * @package app\common\model
 */
class Imgs extends Model
{


    /**
     * 查询一条信息
     *
     * @param array $params
     * @param bool $fields
     * @return array
     */
    public function getInfo(array $params, $fields = true)
    {
        $where = $return = [];

        if (isset($params['id']) && $params['id'] > 0) {
            $where['id'] = $params['id'];
        }

        if (isset($params['img_type']) && !empty($params['img_type'])) {
            $where['img_type'] = $params['img_type'];
        }

        if (isset($params['table_name']) && !empty($params['table_name'])) {
            $where['table_name'] = $params['table_name'];
        }

        if (isset($params['table_id']) && !empty($params['table_id'])) {
            $where['table_id'] = $params['table_id'];
        }

        if (!empty($where)) {
            $res = $this->where($where)->field($fields)->find();
            if (!empty($res)) {
                $return = $res->toArray();
            }
        }

        return $return;
    }

    /**
     * 查询列表
     *
     * @author mayy
     * @date 2017-8-15
     * @param array $params
     * @param $limit
     * @param bool $fields
     * @param bool $is_count
     * @return array
     */
    public function getList(array $params, $limit  = 20, $is_count = false, $fields = true)
    {
        $where = [];

        if (isset($params['id']) && !empty($params['id'])) {
            if (is_array($params['id'])) {
                $where['id'] = ['in', $params['id']];
            } else {
                $where['id'] = $params['id'];
            }
        }

        if (isset($params['img_type']) && !empty($params['img_type'])) {
            $where['img_type'] = $params['img_type'];
        }

        if (isset($params['table_name']) && !empty($params['table_name'])) {
            $where['table_name'] = $params['table_name'];
        }

        if (isset($params['table_id']) && !empty($params['table_id'])) {
            if (is_array($params['table_id'])) {
                $where['table_id'] = ['in', $params['table_id']];
            } else {
                $where['table_id'] = $params['table_id'];
            }
        }

        if (isset($where['order_str'])) {
            $order = $where['order_str'];
        } else {
            $order = 'id desc';
        }

        $count = $this->where($where)->count();
        if ($is_count) {
            return $count;
        }

        $data = $this->where($where)->field($fields)->order($order)->paginate($limit);

        return $data->toArray();
    }

    /**
     * @param $id
     * @param array $req
     * @return bool | integer
     */
    public function updateImg($id, array $req)
    {
        if ($id <= 0) {
            return false;
        }

        $data = $req;

        $res = $this->allowField(true)->where(['id' => $id])->data($data)->update();
        return $res;
    }



    /**
     * 添加一条数据
     *
     * @author mayy
     * @date 2017-8-15
     * @param array $data
     * @return  integer|boolean
     */
    public function addImg(array $data)
    {
        $img_data = $data;
        $img_data['add_time'] = NOW_DATE;

        //不要在同一个实例里面多次新增数据，如果确实需要多次新增，那么可以如下指定
        $res = $this->allowField(true)->data($img_data,true)->isUpdate(false)->save();
        $return = ($res && isset($this->id) && $this->id > 0) ? $this->id : false;

        return $return;
    }

    /**
     * 删除指定条件的图片记录
     *
     * @param array $params
     * @return false | integer
     */
    public function delImgs(array $params)
    {
        if (empty($params)) {
            return false;
        }

        $where = [];

        if (isset($params['id']) && !empty($params['id'])) {
            if (is_array($params['id'])) {
                $where['id'] = ['in', $params['id']];
            } else {
                $where['id'] = $params['id'];
            }
        }

        if (isset($params['table_name']) && !empty($params['table_name'])) {
            $where['table_name'] = $params['table_name'];
        }

        if (isset($params['table_id']) && $params['table_id'] > 0) {
                $where['table_id'] = $params['table_id'];
        }

        if (empty($where)) {
            return false;
        }

        $res = $this->where($where)->delete();
        if ($res === false) {
            \think\Log::record('删除数据失败:' . $this->getLastSql());
        }
        return $res;
    }
}